#include "visiablelabel.h"
#include <QDebug>
#include <QStyle>
Visiablelabel::Visiablelabel(QWidget *parent):
    QLabel(parent)
{
    state_ = Visiable;
    this->setProperty("state","visible");//图标默认为可见状态
    this->style()->polish(this);//刷新样式
    this->update();

}

Visiablelabel::~Visiablelabel()
{

}

void Visiablelabel::mousePressEvent(QMouseEvent *ev)
{
    if (ev->button() == Qt::LeftButton) {
        qDebug() << "左键点击Tabel!";
        qDebug() << "当前状态等于：" << state_;

        if(state_ == UNVisiable | state_ == UNVisiable_Selected)
        {
            state_ = Visiable;
            qDebug() << "切换为可见状态!";

            this->setProperty("state","visible");//图标切换为可见状态

            this->style()->unpolish(this);
            this->style()->polish(this);//刷新样式
            this->update();
            emit clicked(state_);//将可见信号传递给对应的Line Edit
        }
        else if(state_ == Visiable | state_ == Visiable_Selected)
        {
            state_ = UNVisiable;
            setProperty("state","unvisible");//图标切换为不可见状态
            this->style()->unpolish(this);
            this->style()->polish(this);//刷新样式
            this->update();
            emit clicked(state_);//将可见信号传递给对应的Line Edit
        }
        qDebug() << "切换状态等于：" << state_;
    }
    // 调用基类的mouseReleaseEvent以确保正常的事件处理
    QLabel::mouseReleaseEvent(ev);
}

void Visiablelabel::enterEvent(QEvent *ev)
{
    if(state_ == LabelState::Visiable)
    {
        state_ = Visiable_Selected;
        this->setProperty("state","visible_selected");//图标切换为可见-选中状态
        this->style()->polish(this);//刷新样式
        this->update();
    }
    else if(state_ == LabelState::UNVisiable)
    {
        state_ = UNVisiable_Selected;
        this->setProperty("state","unvisible_selected");//图标切换为不可见-选中状态
        this->style()->polish(this);//刷新样式
        this->update();
    }

    QLabel::leaveEvent(ev);
}

void Visiablelabel::leaveEvent(QEvent *ev)
{
    if(state_ == LabelState::Visiable_Selected)
    {
        state_ = Visiable;
        this->setProperty("state","visible");//图标切换为可见状态
        this->style()->polish(this);//刷新样式
        this->update();
    }
    else if(state_ == LabelState::UNVisiable_Selected)
    {
        state_ = UNVisiable;
        this->setProperty("state","unvisible");//图标切换为不可见状态
        this->style()->polish(this);//刷新样式
        this->update();
    }
    QLabel::enterEvent(ev);
}
